extends CharacterBody2D


const SPEED = 300.0

@onready var terrain = $"../Terrain"
@onready var pathfinding = $"../Pathfinding"
@onready var item_manager = $"../ItemManager"

var path = []

func _physics_process(delta):

	# Handle jump.
	if Input.is_action_just_pressed("click"):
		print("click")
		var pos = position / terrain.rendering_quadrant_size
		var targetPos = get_global_mouse_position() / terrain.rendering_quadrant_size
		path = pathfinding.FindPath(pos, targetPos)
		
	if Input.is_action_just_pressed("ui_accept"):
		var pos = position / terrain.rendering_quadrant_size
		var targetPos = item_manager.FindNearestItem(item_manager.ItemCategory.FOOD, position).position / terrain.rendering_quadrant_size
		#print(targetPos)
		path = pathfinding.FindPath(pos, targetPos)
		
	if len(path) > 0:
		var direction = global_position.direction_to(path[0])
		var terrainDifficulty = pathfinding.GetTerrainDifficulty(position / terrain.rendering_quadrant_size)
		velocity = direction * SPEED * (1/terrainDifficulty)
		
		if position.distance_to(path[0]) < SPEED * delta:
			path.remove_at(0)
	else:
		velocity = Vector2i(0, 0)


	move_and_slide()
